<template>
  <div class="article_list">

    <div class="pagination-container">
      <div class="pagination">
        <button 
          @click="prevPage" 
          :disabled="page.pageNo === 1"
          class="page-btn prev-btn">
          上一页
        </button>
        
        <button 
          @click="nextPage" 
          :disabled="page.pageNo === totalPage"
          class="page-btn next-btn">
          下一页
        </button>
      </div>
      <div class="page-info">
        共 {{ total }} 条， {{ totalPage }} 页，当前为第 {{ page.pageNo }} 页 ，该页 {{ articles.length }} 条
      </div>
    </div>

    <div
      v-for="article in articles"
      :key="article.id"
      class="article_item">
      <div class="item-content" @click="goToDetail(article.id)">
        <div class="title">{{ '标题：' + article.title }}</div>
        <div class="meta">
          <span>作者ID：{{ article.userId }}</span>
          <span>浏览：{{ article.views }}</span>
          <span>{{ article.isPublished ? '已发布' : '未发布' }}</span>
          <span v-if="article.isTop">｜置顶</span>
        </div>
      </div>
      <div class="edit-btn" @click="setType(article.id)">
        <button>设置分类</button>
      </div>
      <div class="edit-btn" @click="setTag(article.id)">
        <button>设置标签</button>
      </div>
      <div class="edit-btn" @click="Edit(article.id)">
        <button>编辑</button>
      </div>
      <div class="del-btn" @click="Delete(article.id)">
        <button>删除</button>
      </div>
    </div>

    <button class="article-item create-btn" @click="Create">新建文章</button>
    <EditArticle
      v-model:visible="showModal"
      v-model:isEdit="isEdit"
      v-model:currentArticleId="currentArticleId"
      @save-content="handleUploadSuccess"/>
    <TagForm
      v-model:visible="showTagModal"
      v-model:currentArticleId="currentArticleId"/>
    <TypeForm
      v-model:visible="showTypeModal"
      v-model:currentArticleId="currentArticleId"/>

  </div>
</template>















<script setup>
import { onMounted, ref } from 'vue'
import { getUserArtPage, delUserArtPage } from '@/api/BlogUser'
import { useRouter } from 'vue-router';
import server from '@/api/server'
import EditArticle from '@/components/EditArticle.vue'
import TagForm from '@/components/TagForm.vue';
import TypeForm from '@/components/TypeForm.vue';

const router = useRouter();

const currentArticleId = ref(0)

const showTypeModal = ref(false)
const setType = (articleId) =>{
  console.log(currentArticleId.value)
  currentArticleId.value = articleId;
  showTypeModal.value = true;
}

const showTagModal = ref(false)
const setTag = (articleId) =>{
  currentArticleId.value = articleId;
  showTagModal.value = true;
}


const showModal = ref(false);

const isEdit = ref(false)

const Create = ()=>{
  isEdit.value = false
  showModal.value = true
}

const Edit = (articleId) => {
  currentArticleId.value = articleId
  isEdit.value = true
  showModal.value = true
}

const handleUploadSuccess = () => {
  getPage()
};










const articles = ref([
  {
    id: 7500,
    userId: 13227,
    title: '示例文章标题',
    description: '这是文章的描述部分',
    views: 123,
    isPublished: true,
    isTop: false
  }
])

const total = ref(0);

const totalPage = ref(0)

const page = ref({
  pageNo: 1,
  pageSize: 10
});



const getPage = () => {
  getUserArtPage(server.token, page.value).then((res)=>{
    if (res.data.code == 0) {

      total.value = res.data.data.total
      articles.value = res.data.data.list
      if(total.value <= page.value.pageSize){
        totalPage.value = 1;
      }else{
        let num = total.value%page.value.pageSize
        let num2 = total.value-num
        if(num2==0){
          totalPage.value = 2;
        }else{
          totalPage.value = (num2/page.value.pageSize)+1;
        }
      }
      console.log(res.data.data)

    } else {

      alert(res.data.msg);

    }
  })
}




onMounted(()=>{
  if(server.token == ''){
    router.push("/notLogin")
    return
  }
  getPage()
})

const prevPage = () => {
  page.value.pageNo -= 1
  getPage()
};

const nextPage = () => {
  page.value.pageNo += 1
  getPage()
};

















const Delete = (articleId) => {
  const params = {
    'articleId': articleId
  }
  try {
    delUserArtPage(server.token, params).then((res)=>{
      if(res.data.code == 0){
        alert("删除成功："+res.data.data);
        getPage()
      }else{

        alert("删除失败："+res.data.msg);

      }
    })
  } catch (error) {

    alert("删除失败："+error);

  }
}

const goToDetail = (articleId) => {
  server.currentPageId = articleId;
  router.push("/detail");
}

</script>















<style scoped>
.article_list {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
  font-family: 'Helvetica Neue', Arial, sans-serif;
}

/* 文章卡片样式 */
.article_item {
  display: flex;
  align-items: center;
  padding: 20px;
  margin-bottom: 16px;
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  transition: all 0.3s ease;
  border-left: 4px solid transparent;
}

.article_item:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
  border-left-color: #409EFF;
}

.item-content {
  flex: 1;
  cursor: pointer;
  padding-right: 20px;
  min-width: 0;
}

.title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  font-size: 14px;
  color: #909399;
}

.meta span {
  display: inline-flex;
  align-items: center;
}

.meta span:not(:last-child)::after {
  content: "·";
  margin-left: 12px;
  color: #dcdfe6;
}

/* 操作按钮样式 */
.edit-btn, .del-btn {
  margin-left: 15px;
}

.edit-btn button {
  padding: 6px 12px;
  background: #f0f7ff;
  border: 1px solid #c6e2ff;
  color: #409EFF;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
}

.del-btn button {
  padding: 6px 12px;
  background: #fef0f0;
  border: 1px solid #fbc4c4;
  color: #f56c6c;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
}

.edit-btn button:hover, .del-btn button:hover {
  opacity: 0.8;
  transform: translateY(-1px);
}

/* 新建文章按钮 */
.create-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 16px;
  background: #f5f7fa;
  border: 1px dashed #c0c4cc;
  border-radius: 12px;
  color: #606266;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s;
  margin-top: 20px;
}

.create-btn:hover {
  background: #ecf5ff;
  border-color: #409EFF;
  color: #409EFF;
}

/* 分页样式 */
.pagination-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 30px 0;
  width: 100%;
}

.pagination {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 15px;
}

.page-btn {
  padding: 8px 16px;
  border: 1px solid #dcdfe6;
  background: #ffffff;
  color: #606266;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
  font-size: 14px;
  font-weight: 500;
}

.page-btn:hover:not(:disabled) {
  background: #409EFF;
  color: #ffffff;
  border-color: #409EFF;
}

.page-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.page-info {
  color: #909399;
  font-size: 14px;
  text-align: center;
  line-height: 1.6;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .article_item {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .item-content {
    width: 100%;
    margin-bottom: 12px;
  }
  
  .edit-btn, .del-btn {
    align-self: flex-end;
    margin-left: 8px;
    margin-top: 10px;
  }
  
  .pagination {
    flex-wrap: wrap;
    justify-content: center;
  }
  
  .page-info {
    font-size: 13px;
  }
}
</style>